package cn.demomaster.quickui.view.pickview;

import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.demomaster.quickcompat.util.DisplayUtil;
import com.demomaster.quickcompat.widget.base.QuickGravity;
import com.demomaster.quickcompat.widget.dialog.QuickDialog;

import java.util.ArrayList;
import java.util.List;

import cn.demomaster.quicklogger.QdLog;
import cn.demomaster.quickui.R;

/**
 * @author squirrel桓
 * @date 2023/08/24.
 * description：
 */
public class QuickPicker extends QuickDialog {
    private Button cancelBtn;
    private Button confirmBtn;
    public OnPickListener mListener;
    private String viewFormat;
    private String[] viewFormatTag;
    private PickData pickData;

    public QuickPicker(Context context, Builder builder) {
        super(context,builder);
        this.mListener = builder.listener;
        //array.getColor(styleable.LoopView_topBottomTextColor, -5263441);
        int topBottomTextColor = builder.topBottomTextColor;
        //array.getColor(styleable.LoopView_centerTextColor, -13553359);
        int centerTextColor = builder.centerTextColor;
        //array.getColor(styleable.LoopView_lineColor, -3815995);
        int centerLineColor = builder.centerLineColor;
        int viewTextSize = builder.viewTextSize;
        String def_value = builder.def_value;
        this.viewFormat = builder.viewFormat;
        this.viewFormatTag = builder.viewFormatTag;
        this.pickData = builder.pickData;
        setCancelable(builder.cancelable);
        this.initView();
    }

    private void initView() {
        ViewGroup ll_view_container = findViewById(R.id.ll_view_container);
        QdLog.println("viewFormat=" + viewFormat);
        if (ll_view_container!=null&&!TextUtils.isEmpty(viewFormat)) {
            ll_view_container.removeAllViews();
            String[] strs = viewFormat.split("-");
            int i = 0;
            for (String str : strs) {
                if (str.equals("v")) {
                    LoopView loopView = new LoopView(getContext());
                    loopView.setTag(viewFormatTag[i]);
                    List<PickData.PickDataItem> pickDataItemList = pickData.getDataByTag(viewFormatTag[i]);
                    if (pickDataItemList != null) {
                        List<String> data = new ArrayList<>();
                        for (PickData.PickDataItem pickDataItem : pickDataItemList) {
                            data.add(pickDataItem.getTitle());
                        }
                        loopView.setDataList(data);
                    }
                    loopView.setTextSize(DisplayUtil.dip2px(getContext(), 16));
                    loopView.setCanLoop(false);
                    ViewGroup.LayoutParams layoutParams = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT, 1);
                    ll_view_container.addView(loopView, layoutParams);
                } else if (str.equals("t")) {
                    TextView textView = new TextView(getContext());
                    textView.setText(viewFormatTag[i]);
                    //textView.setTag(viewFormatTag[i]);
                    textView.setTextColor(Color.BLACK);
                    ViewGroup.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                    ll_view_container.addView(textView, layoutParams);
                }
                i++;
            }
        }
    }

    public interface OnPickListener {
        void onSelect(QuickPicker pickerPopWindow, Object[] selectObject, int[] selectPosition);
    }

    public static class Builder<T extends QuickDialog.Builder> extends QuickDialog.Builder<T>{
        public OnPickListener listener;
        public PickData pickData;
        public String viewFormat = "v-t-v-t-v-t";
        public String[] viewFormatTag = new String[]{"YY", "text1", "MM", "text2", "DD", "text3"};//"yyyy年MM月dd日 HH:mm:ss"
        public String def_value = null;
        public int topBottomTextColor = 0xffA9A9A9;//array.getColor(styleable.LoopView_topBottomTextColor, -5263441);
        public int centerTextColor = 0xff11ddaf;//array.getColor(styleable.LoopView_centerTextColor, -13553359);
        public int centerLineColor = 0x00000000;//array.getColor(styleable.LoopView_lineColor, -3815995);
        public int viewTextSize = 25;

        public Builder(Context context,OnPickListener listener) {
            super(context);
            this.listener = listener;
            this.enablePadding = false;
            setContentView(R.layout.layout_common_picker);
            this.isFullScreen=true;
            animationStyleID = R.style.keybored_anim;
            quickGravity = QuickGravity.BOTTOM;
            setCancelable(true);

            this.pickData = new PickData() {
                @Override
                public List<PickDataItem> getDataByTag(String tag, int[] selectIndexs) {
                    if (tag == "YY") {
                        List<PickDataItem> pickDataItemList = new ArrayList<>();
                        for (int i = 0; i < 10; i++) {
                            PickData.PickDataItem pickDataItem = new PickDataItem("" + i);
                            pickDataItemList.add(pickDataItem);
                        }
                        return pickDataItemList;
                    }
                    return null;
                }

                @Override
                public String[] getTags() {
                    return new String[]{"YY", "MM", "DD"};
                }

                @Override
                public int[] getDefautIndex() {
                    return new int[]{0, 0, 0};
                }
            };
        }

        /**
         * 将视图和数据绑定
         *
         * @param viewFormat
         * @param viewFormatTag
         * @return
         */
        public Builder setViewFormat(String viewFormat, String[] viewFormatTag) {
            this.viewFormat = viewFormat;
            this.viewFormatTag = viewFormatTag;
            return this;
        }

        public QuickPicker create() {
            return new QuickPicker(context,this);
        }
    }

}
